using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using WeMakeSP.ImageResizer.Configuration;
using WeMakeSP.ImageResizer.UI.Models;
using WeMakeSP.ImageResizer.UI.Presenters;
using WeMakeSP.ImageResizer.UI.Views;
using WeMakeSP.Platform.SharePoint.Web.UI.Views;

namespace WeMakeSP.ImageResizer.ApplicationPages
{
    /// <summary>
    /// Implementation logic for the image resizer settings page.
    /// </summary>
    public partial class ImageResizerSettings : ViewPageBase<ImageResizerSettingsPresenter, IImageResizerSettingsView>, IImageResizerSettingsView
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ImageResizerSettings"/> class.
        /// </summary>
        public ImageResizerSettings()
        {
            IImageResizerSettingsModel model
               = new ImageResizerSettingsModel(this.Web);

            this.Presenter =
                new ImageResizerSettingsPresenter(this, model);
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets a value indicating whether [configuration enabled].
        /// </summary>
        /// <value><c>True</c> if [configuration enabled]; otherwise, <c>false</c>.</value>
        public bool ConfigurationEnabled
        {
            get
            {
                return 
                    this.enableConfigurationCheckBox.Checked;
            }

            set
            {
                this.enableConfigurationCheckBox.Checked = value;
            }
        }

        /// <summary>
        /// Gets or sets the hours delay.
        /// </summary>
        /// <value>The hours delay.</value>
        public int HoursDelay
        {
            get
            {
                return
                    int.Parse(this.immediateHoursDelay.Text);
            }

            set
            {
                this.immediateHoursDelay.Text = value.ToString();
            }
        }

        /// <summary>
        /// Gets or sets the hour schedule.
        /// </summary>
        /// <value>The hour schedule.</value>
        public int HourSchedule
        {
            get
            {
                return
                    int.Parse(this.scheduleHourStartTimeTextBox.Text);
            }

            set
            {
                this.scheduleHourStartTimeTextBox.Text = value.ToString();
            }
        }

        /// <summary>
        /// Gets the list id.
        /// </summary>
        /// <value>The list id.</value>
        public Guid ListId 
        {
            get
            {
                return 
                    new Guid(Request.QueryString["ListId"]);
            }
        }

        /// <summary>
        /// Gets or sets the width of the max.
        /// </summary>
        /// <value>The width of the max.</value>
        public int MaxWidth 
        {
            get
            {
                return 
                    int.Parse(this.maxWidthTextBox.Text);
            }

            set
            {
                this.maxWidthTextBox.Text = value.ToString();
            }
        }

        /// <summary>
        /// Gets or sets the height of the max.
        /// </summary>
        /// <value>The height of the max.</value>
        public int MaxHeight
        {
            get
            {
                return
                    int.Parse(this.maxHeightTextBox.Text);
            }

            set
            {
                this.maxHeightTextBox.Text = value.ToString();
            }
        }

        /// <summary>
        /// Gets or sets the minutes delay.
        /// </summary>
        /// <value>The minutes delay.</value>
        public int MinutesDelay
        {
            get
            {
                return
                    int.Parse(this.immediateMinutesDelay.Text);
            }

            set
            {
                this.immediateMinutesDelay.Text = value.ToString();
            }
        }

        /// <summary>
        /// Gets or sets the minute schedule.
        /// </summary>
        /// <value>The minute schedule.</value>
        public int MinuteSchedule
        {
            get
            {
                return 
                    int.Parse(this.scheduleMinutesStartTimeTextBox.Text);
            }

            set
            {
                this.scheduleMinutesStartTimeTextBox.Text = value.ToString();
            }
        }

        /// <summary>
        /// Gets or sets the schedule.
        /// </summary>
        /// <value>The schedule.</value>
        public ImageResizerSchedule Schedule
        {
            get
            {
                return
                    (ImageResizerSchedule)Enum.Parse(
                        typeof(ImageResizerSchedule), 
                        this.scheduleRadioButtonList.SelectedValue);
            }

            set
            {
                this.scheduleRadioButtonList.Items.FindByValue(value.ToString()).Selected = true;
            }
        }

        /// <summary>
        /// Gets or sets the mode.
        /// </summary>
        /// <value>The image resizer mode.</value>
        public ImageResizerMode Mode
        {
            get
            {
                return
                   (ImageResizerMode)Enum.Parse(
                    typeof(ImageResizerMode), 
                    this.modeRadionButtonList.SelectedValue);
            }

            set
            {
                this.modeRadionButtonList.Items.FindByValue(value.ToString()).Selected = true;
            }
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.Presenter.OnViewInitialized();
            }

            this.Presenter.OnViewLoaded();
        }

        /// <summary>
        /// Submits the click event handler.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void SubmitClickEventHandler(object sender, EventArgs e)
        {
            this.Presenter.ProcessRequest();
        }

        #endregion
    }
}